<?php
class Repository{

	private static $link = null;

	private static function link(){
		global $link;
		if($link == null){
			$link = mysqli_connect(DB_HOST, DB_USER, DB_PWD, DB_NAME);
			if (!$link) {
			    printf("Connect failed: %s\n", mysqli_connect_error());
			    exit();
			}
			mysqli_query($link,'SET NAMES UTF-8');
			mysqli_autocommit($link, FALSE);
		}
		return $link;
	}

	public static function close(){
		global $link;
		if($link){
			return $link->close();
		}
		return false;
	}

	public static function commit($close_link=TRUE){
		global $link;
		if($link){
			return $link->commit();
			if($close_link){
				$link->close();
			}
		}
		return false;
	}

	public static function rollback(){
		global $link;
		if($link){
			$link->rollback();
		}
	}

	public static function rollback_if_failed(){
		global $link;
		$error = self::error();
		if ($error){
			$link->rollback();
			try {
				throw new Exception("MySQL error :".$error['message'], $error['errno']);    
			} catch(Exception $e) {
				echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br />\r\n";
				echo nl2br($e->getTraceAsString());
				exit();
			}
		}
	}

	public static function error(){
		global $link;
		if ($link) {
		    $error = mysqli_error($link);
		    $errno = mysqli_errno($link);
		    if($error){
		    	return array("message"=>$error, "errno"=>$errno);
		    }
		}
		return NULL;
	}

	public static function expose_error(){
		$error = self::error();
		if($error){
			printf("MySQL Error > Error No. : %s, \r\n %s", $error['errno'],$error['message']);
		}
	}

	public static function prepare($sql){
		return mysqli_prepare(self::link(),$sql); 
	}

	public static function last_insert_id(){
		return mysqli_insert_id(self::link());
	}

	public static function get_result($stmt,$single=FALSE){
    	$rs = $stmt->get_result();
    	if($rs->num_rows == 0) return NULL;
    	if($single&&$rs->num_rows==1){
    		return $rs->fetch_assoc();
    	}
    	$result = array();
		while ($row = $rs->fetch_assoc()){
    		$result[] = $row;
    	}
    	return $result;
	}
}

class User{


	public static function get_by_id($user_id){
		global $SQL;
		$user_id=intval($user_id);
		$stmt = Repository::prepare ($SQL['users']['get_by_id']); 
		$stmt->bind_param('i', $user_id); 
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}

	public static function get_by_email($email){
		global $SQL;
		$stmt = Repository::prepare ($SQL['users']['get_by_email']); 
		$_email=trim(lowercase($email));
		$stmt->bind_param('s', $_email); 
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}

	public static function get_by_email_and_password($email, $password){
		global $SQL;
		$stmt = Repository::prepare ($SQL['users']['get_by_email_and_password']);
		$_email=trim(lowercase($email));
		$_password = md5($password);
		$stmt->bind_param('ss', $_email,$_password); 
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}

	public static function is_exist($email){
		return self::get_by_email($email) != NULL;
	}

	public static function create($name,$password,$email){
		global $SQL;
		$stmt = Repository::prepare($SQL['users']['insert']);
		$password_encrypted=md5($password);
		$stmt->bind_param('sss', $name, $password_encrypted, $email); 
		$stmt->execute(); 
		$user_id = Repository::last_insert_id();
		return $user_id;
	}
}

class UserConfig{

	public static function create($user_id,$amount_per_day,$money){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_configs']['insert']);
		$stmt->bind_param('iii', $user_id, $amount_per_day, $money); 
		$stmt->execute(); 
		$id = Repository::last_insert_id();
		return $id;
	}

	public static function get_by_user_id($user_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_configs']['get_by_user_id']);
		$stmt->bind_param('i', $user_id); 
		$stmt->execute(); 
		return Repository::get_result($stmt,TRUE);
	}
}

class Word{

	public static function create($word,$stmt=NULL){
		if(!$stmt){
			global $SQL;
			$stmt = Repository::prepare($SQL['words']['insert']);
		}
		$stmt->bind_param('s',lowercase($word));
		$stmt->execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function get_by_user_id($user_id=NULL){
		$sql = "SELECT * FROM `voca_words`";
		if($user_id){
			$sql.='RIGHT JOIN voca_user_words ON voca_words.id = voca_user_words.word_id WHERE user_id = ?';
		}
		$stmt = Repository::prepare ($sql); 
		if($user_id){
			$stmt->bind_param('i', $user_id);
		}
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function get_id($word){
		global $SQL;
		$stmt = Repository::prepare ($SQL['words']['get_id']); 
		$w=lowercase($word);
		$stmt->bind_param('s', $w); 
		$stmt->execute(); 
    	$result = Repository::get_result($stmt,TRUE);
    	if($result){
    		return $result['id'];
    	}
    	return 0;
	}

	public static function get_by_id($word_id){
		global $SQL;
		$stmt = Repository::prepare ($SQL['words']['get_by_id']); 
		$stmt->bind_param('i', $word_id); 
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}

	public static function is_word_exist($word){
		return self::get_id($word) > 0;
	}

	public static function is_collected($user_id,$word_id){
		$result = UserWord::get($user_id,$word_id);
		return $result!=NULL?TRUE:FALSE;
	}

	private static function query_sql($user_id=NULL,$order_by=NULL,$filter=NULL,$is_count=FALSE){
		if($is_count){
			$sql = "SELECT count(voca_words.id) `count` FROM `voca_words`";
		}else{
			$sql = "SELECT * FROM `voca_words`";
		}
		if($user_id){
			$sql.=' LEFT JOIN `voca_definitions` ON voca_words.id = voca_definitions.word_id';
			$sql.=' RIGHT JOIN `voca_user_words` ON voca_words.id = voca_user_words.word_id';
			$sql.=' AND voca_user_words.user_id = ?';
			$sql.='  WHERE ( voca_user_words.user_id = ? or voca_definitions.is_standard=1)';
			if($filter=='1'){
				$sql.=' AND voca_user_words.familiarity = 1';
			}elseif ($filter=='2') {
				$sql.=' AND voca_user_words.familiarity = 2';
			}elseif ($filter=='3') {
				$sql.=' AND voca_user_words.familiarity = 3';
			}elseif ($filter=='4') {
				$sql.=' AND voca_user_words.familiarity = 4';
			}elseif ($filter=='5') {
				$sql.=' AND voca_user_words.familiarity = 5';
			}

			if($order_by=='by_time'){
				$sql.=' ORDER BY  voca_user_words.created_at DESC';
			}elseif ($order_by=='by_alphabet'){
				$sql.=' ORDER BY voca_words.word ASC';
			}elseif($order_by=='by_familiarity'){
				$sql.=' ORDER BY voca_user_words.familiarity ASC';
			}
		}else{
			$sql.=' LEFT JOIN `voca_definitions` ON voca_words.id = voca_definitions.word_id WHERE is_standard=1';
		}
		return $sql;
	}

	public static function query_with_definition($user_id=NULL,$order_by=NULL,$filter=NULL,$page=0){
		$sql = self::query_sql($user_id,$order_by,$filter);
		$sql.=' LIMIT '.($page-1)*RPP.', '.RPP;
		$stmt = Repository::prepare ($sql); 
		if($user_id){
			$stmt->bind_param('ii', $user_id,$user_id);
		}
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function count($user_id=NULL,$order_by=NULL,$filter=NULL){
		$sql = self::query_sql($user_id,$order_by,$filter,TRUE);
		$stmt = Repository::prepare ($sql); 
		if($user_id){
			$stmt->bind_param('ii', $user_id,$user_id);
		}
		$stmt->execute(); 
    	$rs=Repository::get_result($stmt,TRUE);
    	if(is_array($rs)&&count($rs)==1){
    		return intval($rs['count']);
    	}
    	return 0;
	}
}


class Definition{

	public static function is_exist($word_id,$user_id=NULL){
		$result = self::get($word_id,$user_id);
		return $result!=NULL?TRUE:FALSE;;
	}

	public static function create($word_id, $user_id, $definition, $stmt=NULL){
		if(!$stmt){
			global $SQL;
			$stmt = Repository::prepare($SQL['word_definitions']['insert']);
		}
		$stmt->bind_param("iis",$word_id,$user_id,$definition);
		$stmt->execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function update($word_id, $user_id, $definition, $stmt){
		if(!$stmt){
			global $SQL;
			$stmt = Repository::prepare($SQL['word_definitions']['update']);
		}
		$stmt->bind_param("sii", $definition,$word_id, $user_id);
		$stmt->execute();
		return $stmt->affected_rows == 1;
	}

	public static function get($word_id,$user_id=NULL){
		$sql = "SELECT * FROM `voca_definitions` WHERE word_id = ?";
		if($user_id){
			$sql.=" AND user_id = ?";
		}
		$stmt = Repository::prepare ($sql); 
		if($user_id){
			$stmt->bind_param('ii', $word_id, $user_id);
		}else{
			$stmt->bind_param('i', $word_id);
		}
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}
}


class UserWord{

	public static function create($user_id, $word_id, $stmt=NULL){
		if(!$stmt){
			global $SQL;
			$stmt = Repository::prepare($SQL['user_words']['insert']);
		}
		$stmt->bind_param('ii',$user_id,$word_id);
		$stmt->execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function remove($user_id,$word_id){
		global $SQL;
		$stmt = Repository::prepare ($SQL['user_words']['delete']); 
		$stmt->bind_param('ii', $user_id,$word_id);
		$stmt->execute(); 
		return $stmt->affected_rows == 1;
	}

	public static function get($user_id,$word_id){
		$sql = "SELECT * FROM `voca_user_words` WHERE user_id = ? and word_id = ?";
		$stmt = Repository::prepare ($sql); 
		$stmt->bind_param('ii', $user_id,$word_id);
		$stmt->execute(); 
    	return Repository::get_result($stmt,TRUE);
	}

	public static function set_familiarity($familiarity, $user_id, $word_id, $stmt=NULL){
		if(!$stmt){
			global $SQL;
			$stmt = Repository::prepare ($SQL['user_words']['update']); 
		}
		$stmt->bind_param('iii', $familiarity,$user_id,$word_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows;
	}

	public static function incr_familiarity($user_id,$word_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_words']['incr_familiarity']);
		$stmt->bind_param('ii', $user_id,$word_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows; 
	}
}

class Sentence{

	public static function remove($sentence_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['sentences']['delete']);
		$stmt -> bind_param('i',$sentence_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows == 1;
	}

	public static function remove_user_sentence($user_id,$sentence_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_sentences']['delete']);
		$stmt->bind_param('ii', $user_id,$sentence_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows == 1;
	}

	public static function get($word_id,$user_id){
		$sql='SELECT view_sentences.sentence_id id,word_id,user_id,is_creator,sentence,translation,num_of_like,num_of_dislike';
		$sql.=' FROM `view_sentences`';
		$sql.=' LEFT JOIN `voca_user_sentences` ON view_sentences.sentence_id = voca_user_sentences.sentence_id and user_id=?';
		$sql.=' WHERE word_id=?';
		$sql.=' AND view_sentences.sentence_id NOT IN (SELECT sentence_id FROM `voca_user_sentence_blacklist` WHERE user_id=?)';
		$sql.=' ORDER BY num_of_like DESC';
		$stmt = Repository::prepare ($sql); 
		$stmt->bind_param('iii', $user_id, $word_id, $user_id);
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function add_to_blacklist($sentence_id, $user_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_sentence_blacklist']['insert']);
		$stmt->bind_param('ii', $sentence_id,$user_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
    	return Repository::last_insert_id();
	}

	public static function get_mime($word_id,$user_id,$is_creator=1){
		$sql='SELECT view_sentences.sentence_id id,word_id,user_id,is_creator,sentence,translation,num_of_like,num_of_dislike';
		$sql.=' FROM `view_sentences`';
		$sql.=' INNER JOIN `voca_user_sentences` on  view_sentences.sentence_id = voca_user_sentences.sentence_id';
		$sql.=' WHERE view_sentences.word_id = ? AND voca_user_sentences.user_id = ? and is_creator=?';
		$stmt = Repository::prepare ($sql); 
		$stmt->bind_param('iii', $word_id, $user_id,$is_creator);
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function create_user_sentence($user_id,$sentence_id,$is_creator){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_sentences']['insert']);
		$stmt -> bind_param('iii',$user_id,$sentence_id,$is_creator);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function create($word_id,$sentence,$translation,$user_id){
		global $SQL;
		$sentence = compact_string(strip_tags($sentence));
		$translation = compact_string(strip_tags($translation));
		$stmt = Repository::prepare($SQL['sentences']['insert']);
		$stmt -> bind_param('iss',$word_id,$sentence,$translation);
		$stmt -> execute();
		Repository::rollback_if_failed();
		$sentence_id = Repository::last_insert_id();
		self::create_user_sentence($user_id,$sentence_id,TRUE);
		return $sentence_id;
	}

	public static function update($sentence_id, $sentence, $translation){
		global $SQL;
		$sentence = compact_string(strip_tags($sentence));
		$translation = compact_string(strip_tags($translation));
		$stmt = Repository::prepare($SQL['sentences']['update']);
		$stmt -> bind_param('ssi',$sentence, $translation, $sentence_id);
		$stmt -> execute();
		return $stmt -> affected_rows > 0;
	}

	public static function sentences_collected_by($sentence_ids,$user_id){
		$sql='SELECT distinct sentence_id FROM `voca_user_sentences` WHERE user_id = ? AND sentence_id IN (';
		if(is_array($sentence_ids)){
			foreach ($sentence_ids as $sentence_id) {
				$sql.=intval($sentence_id).',';
			}
		}
		$sql=substr_replace($sql,")",-1);
		$stmt = Repository::prepare($sql);
		$stmt -> bind_param('i',$user_id);
		$stmt -> execute();
		return Repository::get_result($stmt);
	}
}


class Note{

	public static function remove($note_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['notes']['delete']);
		$stmt -> bind_param('i',$note_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows == 1;
	}

	public static function remove_user_note($user_id,$note_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_notes']['delete']);
		$stmt->bind_param('ii', $user_id,$note_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
		return $stmt->affected_rows == 1;
	}

	public static function get($word_id,$user_id){
		$sql='SELECT view_notes.note_id id,word_id,user_id,is_creator,note,num_of_like,num_of_dislike';
		$sql.=' FROM `view_notes`';
		$sql.=' LEFT JOIN `voca_user_notes` ON view_notes.note_id = voca_user_notes.note_id and user_id=?';
		$sql.=' WHERE word_id=?';
		$sql.=' AND view_notes.note_id NOT IN (SELECT note_id FROM `voca_user_note_blacklist` WHERE user_id=?)';
		$sql.=' ORDER BY num_of_like DESC';
		$stmt = Repository::prepare ($sql); 
		$stmt->bind_param('iii', $user_id, $word_id, $user_id);
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function add_to_blacklist($note_id, $user_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_note_blacklist']['insert']);
		$stmt->bind_param('ii', $note_id,$user_id);
		$stmt->execute(); 
		Repository::rollback_if_failed();
    	return Repository::last_insert_id();
	}

	public static function get_mime($word_id,$user_id,$is_creator=1){
		$sql='SELECT view_notes.note_id id,word_id,user_id,is_creator,note,num_of_like,num_of_dislike';
		$sql.=' FROM `view_notes`';
		$sql.=' INNER JOIN `voca_user_notes` on  view_notes.note_id = voca_user_notes.note_id';
		$sql.=' WHERE view_notes.word_id = ? AND voca_user_notes.user_id = ? and is_creator=?';
		$stmt = Repository::prepare ($sql); 
		$stmt->bind_param('iii', $word_id, $user_id,$is_creator);
		$stmt->execute(); 
    	return Repository::get_result($stmt);
	}

	public static function create_user_note($user_id,$note_id,$is_creator){
		global $SQL;
		$stmt = Repository::prepare($SQL['user_notes']['insert']);
		$stmt -> bind_param('iii',$user_id,$note_id,$is_creator);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function create($word_id,$note,$user_id){
		global $SQL;
		$note = compact_string(strip_tags($note));
		$stmt = Repository::prepare($SQL['notes']['insert']);
		$stmt -> bind_param('is',$word_id,$note);
		$stmt -> execute();
		Repository::rollback_if_failed();
		$note_id = Repository::last_insert_id();
		self::create_user_note($user_id,$note_id,TRUE);
		return $note_id;
	}

	public static function update($note_id, $note){
		global $SQL;
		$note = compact_string(strip_tags($note));
		$stmt = Repository::prepare($SQL['notes']['update']);
		$stmt -> bind_param('si',$note, $note_id);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return $stmt -> affected_rows > 0;
	}

	public static function notes_collected_by($note_ids,$user_id){
		$sql='SELECT distinct note_id FROM `voca_user_notes` WHERE user_id = ? AND note_id IN (';
		if(is_array($note_ids)){
			foreach ($note_ids as $note_id){
				$sql.=intval($note_id).',';
			}
		}
		$sql=substr_replace($sql,")",-1);
		$stmt = Repository::prepare($sql);
		$stmt -> bind_param('i',$user_id);
		$stmt -> execute();
		return Repository::get_result($stmt);
	}
}

class Group{

	public static function get_by_id($group_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['groups']['get_by_id']);
		$stmt -> bind_param('i',$group_id);
		$stmt -> execute();
		return Repository::get_result($stmt,TRUE);
	}

	public static function get_groups(){
		global $SQL;
		$stmt = Repository::prepare($SQL['groups']['get_all']);
		$stmt -> execute();
		return Repository::get_result($stmt);
	}

}

class Topic{

	public static function create($group_id,$user_id,$title,$content){
		global $SQL;
		$group_id = intval($group_id);
		$user_id = intval($user_id);
		$tilte = trim(strip_tags($title));
		$content = trim(strip_tags($content));
		$stmt = Repository::prepare($SQL['group_topics']['insert']);
		$stmt -> bind_param('iiss',$group_id,$user_id,$title,$content);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function update($topic_id,$title,$content,$user_id){
		global $SQL;
		$topic_id = intval($topic_id);
		$tilte = trim(strip_tags($title));
		$content = trim(strip_tags($content));
		$stmt = Repository::prepare($SQL['group_topics']['update']);
		$stmt -> bind_param('ssii',$title,$content,$topic_id,$user_id);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return $stmt->affected_rows==1;
	}

	public static function remove($id,$user_id){
		global $SQL;
		$id = intval($id);
		$stmt = Repository::prepare($SQL['group_topics']['delete']);
		$stmt -> bind_param('ii',$id,$user_id);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return $stmt->affected_rows==1;
	}

	public static function get_by_id($topic_id){
		global $SQL;
		$stmt = Repository::prepare($SQL['group_topics']['get_by_id']);
		$stmt -> bind_param('i',$topic_id);
		$stmt -> execute();
		return Repository::get_result($stmt,TRUE);
	}

	public static function getCount($group_id=NULL){
		global $SQL;
		$sql=$SQL['group_topics']['get_count'];
		if($group_id){
			$sql.=" WHERE group_id = ?";
		}
		$stmt = Repository::prepare($sql);
		if($group_id){
			$stmt -> bind_param('i',$group_id);
		}
		$stmt -> execute();
		return Repository::get_result($stmt,TRUE)['count'];
	}

	public static function get_by_group_id($group_id=NULL,$page=1){
		$page=$page<1?1:$page;
		$sql="SELECT t.id, group_id, is_top,allow_of_reply,g.name group_name, title, t.user_id, t.created_at, u.name created_by,vt.*,count, IFNULL(replied_at,t.created_at) as order_time".
			" FROM `voca_group_topics` t".
			" LEFT JOIN ( SELECT topic_id,p1.id reply_id, p1.created_at replied_at,p1.user_id reply_user_id,name replied_by".
			" FROM voca_group_replies p1".
			" INNER JOIN (".
			" SELECT MAX(pi.id) AS max_reply_id".
			" FROM voca_group_replies pi".
			" GROUP BY pi.topic_id".
			" ) p2 ON (p1.id = p2.max_reply_id)".
			" LEFT JOIN voca_users ON user_id = voca_users.id".
			" ) vt ON t.id = vt.topic_id".
			" LEFT JOIN (".
    		" SELECT topic_id,count(1) count FROM voca_group_replies".
    		" GROUP BY topic_id".
			") vt2 ON t.id = vt2.topic_id".
			" LEFT JOIN voca_users u ON user_id = u.id".
			" LEFT JOIN voca_groups g ON group_id = g.id";
		if($group_id){
			$sql.=" WHERE group_id=?";
		}
		$sql.=" ORDER BY is_top DESC, order_time DESC";
		$sql.=" LIMIT ".(($page-1)*RPP).", ".RPP;
		$stmt = Repository::prepare($sql);
		if($group_id){
			$stmt -> bind_param('i',$group_id);
		}
		$stmt -> execute();
		return Repository::get_result($stmt);
	}
}

class Reply{

	public static function create($topic_id,$user_id,$content){
		global $SQL;
		$topic_id = intval($topic_id);
		$user_id = intval($user_id);
		$content = trim(strip_tags($content));
		$stmt = Repository::prepare($SQL['group_replies']['insert']);
		$stmt -> bind_param('iis',$topic_id,$user_id,$content);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return Repository::last_insert_id();
	}

	public static function update($id,$content,$user_id){
		global $SQL;
		$id = intval($id);
		$user_id = intval($user_id);
		$content = trim(strip_tags($content));
		$stmt = Repository::prepare($SQL['group_replies']['update']);
		$stmt -> bind_param('sii',$content,$id,$user_id);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return $stmt->affected_rows==1;
	}

	public static function remove($id,$user_id){
		global $SQL;
		$id = intval($id);
		$stmt = Repository::prepare($SQL['group_replies']['delete']);
		$stmt -> bind_param('ii',$id,$user_id);
		$stmt -> execute();
		Repository::rollback_if_failed();
		return $stmt->affected_rows==1;
	}


	public static function getCount($topic_id){
		global $SQL;
		$sql=$SQL['group_replies']['get_count'];
		$stmt = Repository::prepare($sql);
		$stmt -> bind_param('i',$topic_id);
		$stmt -> execute();
		return Repository::get_result($stmt,TRUE)['count'];
	}


	public static function get_by_topic_id($topic_id,$page=1){
		global $SQL;
		$page=$page<1?1:$page;
		$sql= $SQL['group_replies']['get_by_topic_id']." LIMIT ".(($page-1)*RPP).", ".RPP;
		$stmt = Repository::prepare($sql);
		$stmt -> bind_param('i',$topic_id);
		$stmt -> execute();
		return Repository::get_result($stmt);
	}

}
?>